import { getDisclosureApiData } from "../../../api/GetDataSourceApi";
import moment from "moment";
/**
 * 获取表头信息
 * @param params
 */
export const getColumnHeader = async (params) => {
    const { tname, data_id, dbName, fields, where, order } = params;
    const { data } = await getDisclosureApiData(tname, data_id, dbName, fields, where, order, true, 0);
    let columnData = [];
    let dataType = data;
    dataType.map((item) => {
        let fieldType = item.FieldType;
        // 补丁,是否关键账户将 bool类型转换为string类型, 详情Bug参考下面的补丁说明
        if (item.FieldName === "是否关键账户") {
            fieldType = "string";
        }
        const defaultColumn = {
            dataField: item.FieldName,
            caption: item.FieldCaption,
            fixed: false,
            // minWidth: "130px",
            // maxWidth: "300px",
            FieldType: fieldType,
        };
        if (fieldType === "number") {
            defaultColumn["format"] = ",##0.00";
        }
        columnData.push(defaultColumn);
        return item;
    });
    if (columnData.length > 0) {
        columnData[0].fixed = true; // 冻结主键第一列
    }
    return { dataType, columnData };
};
/**
 * 获取表体信息
 * @param params
 * @param projectSearchValue
 */
export const getTableData = async (params, projectSearchValue) => {
    const { tname, data_id, dbName, fields, where, order } = params;
    let { data } = await getDisclosureApiData(tname, data_id, dbName, fields, where, order, false, 0);
    if (data) {
        data = data.map((item) => {
            // 补丁, 因为dev会将 true , false  直接解析成 checkbox , 而checkbox 会导致虚拟滚动行高出现问题.
            if (item["是否关键账户"] !== undefined) {
                item["是否关键账户"] = item["是否关键账户"] ? "是" : "否";
            }
            if (item["编辑时间"] !== undefined) {
                item["编辑时间"] = moment(item["编辑时间"]).format("YYYY-MM-DD hh:mm:ss");
            }
            return item;
        });
        if (!projectSearchValue) {
            return data;
        }
        else {
            //模糊检索
            const filterData = fuzzyQuery(data, projectSearchValue);
            return filterData;
        }
    }
    else {
        return [];
    }
};
/**
 * 模糊查询
 * @param list
 * @param keyWord
 */
const fuzzyQuery = (list, keyWord) => {
    const arr = [];
    for (var i = 0; i < list.length; i++) {
        if (list[i].主键 && list[i].主键.indexOf(keyWord) >= 0) {
            arr.push(list[i]);
        }
    }
    return arr;
};
